package by.pavlyukevich.paymentsystem.page;

import java.util.ArrayList;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.event.Broadcast;
import org.apache.wicket.event.IEvent;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.ResourceModel;

import by.pavlyukevich.paymentsystem.add.ErrorEvent;
import by.pavlyukevich.paymentsystem.add.PanelUpdateEvent;
import by.pavlyukevich.paymentsystem.panel.AccountListPanel;
import by.pavlyukevich.paymentsystem.panel.TransactionListPanel;
import by.pavlyukevich.paymentsystem.panel.card.CardLimitListPanel;
import by.pavlyukevich.paymentsystem.panel.card.CardListPanel;
import by.pavlyukevich.paymentsystem.panel.user.ContractorListPanel;
import by.pavlyukevich.paymentsystem.panel.user.IndividualListPanel;
import by.pavlyukevich.paymentsystem.panel.user.UserAccountListPanel;

import com.googlecode.wicket.jquery.ui.widget.menu.IMenuItem;
import com.googlecode.wicket.jquery.ui.widget.menu.Menu;
import com.googlecode.wicket.jquery.ui.widget.menu.MenuItem;

public class AdminPage extends BaseLayout {

	@Override
	protected void onInitialize() {
		super.onInitialize();

		WebMarkupContainer adminContent = new WebMarkupContainer("admin-content") {

			@Override
			public void onEvent(IEvent<?> event) {
				if (event.getPayload() instanceof PanelUpdateEvent) {
					PanelUpdateEvent pUpdateEvent = (PanelUpdateEvent) event.getPayload();
					addOrReplace(pUpdateEvent.getPanel());
					pUpdateEvent.getTarget().add(pUpdateEvent.getPanel());
				}
			}
		};
		adminContent.setOutputMarkupId(true);

		adminContent.add(new FeedbackPanel("feedback") {

			@Override
			public void onEvent(IEvent<?> event) {
				if (event.getPayload() instanceof ErrorEvent) {
					ErrorEvent errorEvent = (ErrorEvent) event.getPayload();
					errorEvent.getTarget().add(this);
				}
				if (event.getPayload() instanceof PanelUpdateEvent) {
					PanelUpdateEvent panelUpdateEvent = (PanelUpdateEvent) event.getPayload();
					panelUpdateEvent.getTarget().add(this);
				}
			}
		}.setOutputMarkupId(true));

		WebMarkupContainer contentPanel = new IndividualListPanel("ac-panel");
		adminContent.add(contentPanel);

		add(adminContent);
		add(new Menu("admin-menu", navigateMenu()));

	}

	public List<IMenuItem> navigateMenu() {
		List<IMenuItem> itemList = new ArrayList<IMenuItem>();

		itemList.add(new MenuItem(new ResourceModel("l.individual.title")) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target,
						new IndividualListPanel("ac-panel")));
			}
		});

		itemList.add(new MenuItem(new ResourceModel("l.account.title")) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target,
						new AccountListPanel("ac-panel")));
			}
		});

		itemList.add(new MenuItem(new ResourceModel("l.card.title")) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target, new CardListPanel(
						"ac-panel")));
			}
		});

		itemList.add(new MenuItem(new ResourceModel("l.userAccount.title")) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target,
						new UserAccountListPanel("ac-panel")));
			}
		});

		itemList.add(new MenuItem(new ResourceModel("l.contractor.title")) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target,
						new ContractorListPanel("ac-panel")));
			}
		});

		itemList.add(new MenuItem(new ResourceModel("l.cardLimit.title")) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target,
						new CardLimitListPanel("ac-panel")));
			}
		});

		itemList.add(new MenuItem(new ResourceModel("l.transaction.title")) {
			@Override
			public void onClick(AjaxRequestTarget target) {
				send(getPage(), Broadcast.BREADTH, new PanelUpdateEvent(target,
						new TransactionListPanel("ac-panel")));
			}
		});

		return itemList;
	}
}
